//
//  ImageGalleryView.m
//  AutoShowView
//
//  Created by 冷悦 on 16/1/31.
//  Copyright © 2016年 冷悦. All rights reserved.
//

#import "ImageGalleryView.h"
#import "AutoShowView.h"

#pragma mark - Default Model Implementation
@implementation ImageGalleryModel

@end

@interface ImageGalleryView ()

@property (nonatomic, strong) UILabel *introductionLabel;

@end

@implementation ImageGalleryView

#pragma mark - Override
- (void)drawRect:(CGRect)rect{
    CGFloat width = rect.size.width;
    CGFloat height = rect.size.height;
    if (self.introductionLabel.text != nil) {
        CGFloat autoShowViewHeight = height * 3 / 4;
        CGFloat introductionLabelHeight = height / 4;
        CGRect autoShowViewFrame = CGRectMake(0, 0, width, autoShowViewHeight);
        CGRect introductionLabelFrame = CGRectMake(0, autoShowViewHeight, width, introductionLabelHeight);
        self.autoShowView.frame = autoShowViewFrame;
        self.introductionLabel.frame = introductionLabelFrame;
    }else{
        CGFloat autoShowViewHeight = height;
        CGRect autoShowViewFrame = CGRectMake(0, 0, width, autoShowViewHeight);
        self.autoShowView.frame = autoShowViewFrame;
    }
}

#pragma mark - Event Response
- (void)touchedAction:(UITapGestureRecognizer *)tapGesture{
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(didTouched:)]) {
        AutoShowView *autoShowView = (AutoShowView *)tapGesture.view;
        [self.delegate didTouched:autoShowView];
    }
}

#pragma mark - Public
- (void) updateImageView{
    [self.autoShowView updateImageView];
}

#pragma mark - Getters
- (AutoShowView *)autoShowView{
    if (_autoShowView == nil) {
        _autoShowView = [AutoShowView new];
        _autoShowView.backgroundColor = SelectBackgroundColor230;
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self
                                                                                    action:@selector(touchedAction:)];
        _autoShowView.userInteractionEnabled = YES;
        [_autoShowView addGestureRecognizer:tapGesture];
        [self addSubview:_autoShowView];
    }
    
    return _autoShowView;
}

- (UILabel *)introductionLabel{
    if (_introductionLabel == nil) {
        _introductionLabel = [UILabel new];
        _introductionLabel.textAlignment = NSTextAlignmentCenter;
        _introductionLabel.backgroundColor = [UIColor whiteColor];
        [self addSubview:_introductionLabel];
    }
    
    return _introductionLabel;
}

#pragma mark - Setters
- (void)setModel:(id<ImageGalleryModelDelegate>)model{
    _model = model;
    NSArray *images = model.images;
    NSTimeInterval timeInterval = model.timeInterval;
    NSString *introduction = model.introduction;
    self.introductionLabel.text = introduction;
    self.autoShowView.timeInterval = timeInterval;
    self.autoShowView.images = images;
}

@end





























